package educrypt.gui.components;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import educrypt.gui.view.EditorTextOptionsView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

import javax.swing.JScrollPane;
import java.awt.Component;

class EditorTextLanguageDialog extends EducryptDialog implements
		EditorTextOptionsView {

	private static final long serialVersionUID = 8200696352381662957L;

	private final JPanel contentPanel = new JPanel();
	private JTextField txNewTextLanguage;
	private JList lstStoredTextLanguages;
	private JButton btnAddTextLanguage, btnRemoveTextLanguage;
	private JScrollPane scrollPane;

	public EditorTextLanguageDialog(Window owner) {
		super(owner);
		setTitle(Messages.getString("guiTextLanguageEditor")); //$NON-NLS-1$

		getContentPane().setLayout(new BorderLayout());
		// contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblEditorTextLanguage = new JLabel(
				Messages.getString("guiTextLanguageEditor"));
		lblEditorTextLanguage.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblEditorTextLanguage.setBounds(10, 11, 378, 24);
		contentPanel.add(lblEditorTextLanguage);

		JLabel lblNewTextLanguage = new JLabel(Messages.getString("guiNewTextLanguage"));
		lblNewTextLanguage.setBounds(10, 62, 111, 14);
		contentPanel.add(lblNewTextLanguage);

		txNewTextLanguage = new JTextField();
		txNewTextLanguage.setColumns(10);
		txNewTextLanguage.setBounds(131, 57, 266, 24);
		contentPanel.add(txNewTextLanguage);

		ImageIcon iconAdd = new ImageIcon(EduCryptResources.getImageURL("plus24.png"));
		ImageIcon iconRemove = new ImageIcon(EduCryptResources.getImageURL("remove24color.png"));

		btnAddTextLanguage = new JButton(iconAdd);
		btnAddTextLanguage.setBounds(407, 57, 24, 24);
		contentPanel.add(btnAddTextLanguage);

		btnRemoveTextLanguage = new JButton(iconRemove);
		btnRemoveTextLanguage.setBounds(407, 92, 24, 24);
		contentPanel.add(btnRemoveTextLanguage);

		JLabel lblStoredTextLanguages = new JLabel(
				Messages.getString("guiStoredLanguages"));
		lblStoredTextLanguages.setBounds(10, 92, 111, 42);
		contentPanel.add(lblStoredTextLanguages);
		
		scrollPane = new JScrollPane((Component) null);
		scrollPane.setBounds(131, 94, 266, 160);
		contentPanel.add(scrollPane);
		
				lstStoredTextLanguages = new JList();
				scrollPane.setViewportView(lstStoredTextLanguages);
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnAddTextLanguage.setActionCommand(ADD_ITEM);
		btnAddTextLanguage.addActionListener(ctr);

		btnRemoveTextLanguage.setActionCommand(REMOVE_ITEM);
		btnRemoveTextLanguage.addActionListener(ctr);
	}

	@Override
	public String getNewItem() {
		return txNewTextLanguage.getText();
	}

	@Override
	public void showStoredItems(List<String> textTypes) {
		lstStoredTextLanguages.setListData(textTypes.toArray());
	}

	@Override
	public String getSelectedItem() {
		Object selectedValue = lstStoredTextLanguages.getSelectedValue();
		String selectedType = (selectedValue != null) ? selectedValue
				.toString() : "";
		return selectedType;
	}

	@Override
	public void removeNewItem() {
		txNewTextLanguage.setText("");
	}
}
